package com.springchang.leetcode.array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * leetcode 448 找到所有数组中消失的数字
 */
public class NumbersDisappeared_448 {

    /*利用数组来记录元素出现的次数*/
    public static List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> result = new ArrayList<>();

        //桶
        int[] bukects = new int[nums.length];

        for (int i = 0; i < nums.length; i++) {
            bukects[nums[i]-1] ++;
        }

        for (int i = 0; i < bukects.length; i++) {
            if (bukects[i] == 0) {
                result.add(i+1);
            }
        }
        return result;
    }

    public static List<Integer> findDisappearedNumbersMode(int[] nums) {
        List<Integer> result = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            //求模，计算nums[i]的原始值，因为nums[i]可能已经被修改过
            int mode = nums[i];
            if(mode > nums.length) {
                mode %= nums.length;
            }
            if(mode == 0) {
                mode = nums.length;
            }
            nums[mode-1] += nums.length;
        }

        //遍历结果数组，如果数组的值小于等于N，则表示数组中的小标等于他们的值得数据未被修改过
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] <= nums.length) {
                result.add(i+1);
            }
        }

        return result;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{4,3,2,7,8,2,3,1};
        List<Integer> list = findDisappearedNumbersMode(nums);
        System.out.println(String.format("数组的内容: %s", Arrays.toString(list.toArray())));
    }
}
